<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>searchcode tests</title>
  <link rel="stylesheet" href="./qunit-git.css">
</head>
<body>
  <div class="container" style="display:none;"></div>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
  <script src="./qunit-git.js"></script>
  <script src="../../main/resources/public/js/underscore-min.js"></script>
  <script src="../../main/resources/public/js/mithril.min.js"></script>
  <script src="../../main/resources/public/js/script.js"></script>
<script>
/////////////////////////////////////////////////////////////////////
QUnit.module('HelperModel');
QUnit.test('humanise', function( assert ) {
  assert.equal(HelperModel.humanise(1), '1 Day');
  assert.equal(HelperModel.humanise(2), '2 Days');
  assert.equal(HelperModel.humanise(3), '3 Days');
  assert.equal(HelperModel.humanise(7), '7 Days');
  assert.equal(HelperModel.humanise(30), '1 Month');
  assert.equal(HelperModel.humanise(60), '2 Months');
  assert.equal(HelperModel.humanise(365), '1 Year');
  assert.equal(HelperModel.humanise(365 * 2), '2 Years');

  assert.equal(HelperModel.humanise(56), '1 Month 26 Days');
  assert.equal(HelperModel.humanise(400), '1 Year 1 Month 5 Days');
  assert.equal(HelperModel.humanise(800), '2 Years 2 Months 10 Days');
});

QUnit.test('getDateSpan', function( assert ) {
  assert.equal(HelperModel.getDateSpan(new Date(), new Date()).length, 1);
  assert.equal(HelperModel.getDateSpan(new Date(2000, 01, 01), new Date(2000, 01, 10)).length, 10);
});

QUnit.test('yearMonthDayToDate', function( assert ) {
  assert.equal(HelperModel.yearMonthDayToDate('2000-12-12').getFullYear(), 2000);
  assert.equal(HelperModel.yearMonthDayToDate('2000-12-12').getMonth(), 11);
  assert.equal(HelperModel.yearMonthDayToDate('2000-12-12').getDate(), 12);
});

QUnit.test('yearMonthDayToDate', function( assert ) {
  assert.equal(HelperModel.yearMonthDayToDate('2000-12-12').getFullYear(), 2000);
  assert.equal(HelperModel.yearMonthDayToDate('2000-12-12').getMonth(), 11);
  assert.equal(HelperModel.yearMonthDayToDate('2000-12-12').getDate(), 12);
});

/////////////////////////////////////////////////////////////////////
QUnit.module('SearchModel');
QUnit.test('clearfilters', function( assert ) {
  SearchModel.facetfilters({'something': 'else'});
  SearchModel.pathvalue('something');

  assert.ok('something' in SearchModel.facetfilters());
  assert.equal(SearchModel.pathvalue(), 'something');

  SearchModel.clearfilters();

  assert.notOk('something' in SearchModel.facetfilters());
  assert.equal(SearchModel.pathvalue(), '');
});

QUnit.test('togglecompact', function( assert ) {
  var current = SearchModel.compactview();
  SearchModel.togglecompact();
  assert.notEqual(current, SearchModel.compactview());
  assert.notEqual(current, JSON.parse(localStorage.getItem('togglecompact')));
});

QUnit.test('toggle_instant', function( assert ) {
  var current = SearchModel.filterinstantly();
  SearchModel.toggle_instant();
  assert.notEqual(current, SearchModel.filterinstantly());
  assert.notEqual(current, JSON.parse(localStorage.getItem('toggleinstant')));
});

QUnit.test('get_string_title', function( assert ) {
  SearchModel.query('my search string');
  assert.equal(SearchModel.get_string_title(), '"my search string"');
  
  SearchModel.activerepositoryfilters(['one']);
  assert.equal(SearchModel.get_string_title(), '"my search string" filtered by repository "one"');
  SearchModel.activerepositoryfilters(['one', 'two']);
  assert.equal(SearchModel.get_string_title(), '"my search string" filtered by repositories "one, two"');

  SearchModel.activelangfilters(['java']);
  assert.equal(SearchModel.get_string_title(), '"my search string" filtered by repositories "one, two" and language "java"');
  SearchModel.activelangfilters(['java', 'python']);
  assert.equal(SearchModel.get_string_title(), '"my search string" filtered by repositories "one, two" and languages "java, python"');

  SearchModel.activeownfilters(['person1']);
  assert.equal(SearchModel.get_string_title(), '"my search string" filtered by repositories "one, two" and languages "java, python" and owner "person1"');
  SearchModel.activeownfilters(['person1', 'person2']);
  assert.equal(SearchModel.get_string_title(), '"my search string" filtered by repositories "one, two" and languages "java, python" and owners "person1, person2"');
});

QUnit.test('toggle_filter', function( assert ) {
  SearchModel.facetfilters({});
  SearchModel.toggle_filter('language', 'test');
  assert.ok(SearchModel.filter_exists('language', 'test'));
  SearchModel.toggle_filter('language', 'test');
  assert.notOk(SearchModel.filter_exists('language', 'test'));

  SearchModel.toggle_filter('repo', 'test');
  assert.ok(SearchModel.filter_exists('repo', 'test'));
  SearchModel.toggle_filter('repo', 'test');
  assert.notOk(SearchModel.filter_exists('repo', 'test'));

  SearchModel.toggle_filter('owner', 'test');
  assert.ok(SearchModel.filter_exists('owner', 'test'));
  SearchModel.toggle_filter('owner', 'test');
  assert.notOk(SearchModel.filter_exists('owner', 'test'));

  SearchModel.facetfilters({});
  SearchModel.toggle_filter('language', 'test');
  assert.ok(SearchModel.facetfilters()['language']);
  assert.equal('test', SearchModel.facetfilters()['language'][0]);

  SearchModel.toggle_filter('language', 'test');
  SearchModel.toggle_filter('language', 'test2');
  assert.ok(SearchModel.facetfilters()['language']);
  assert.equal('test2', SearchModel.facetfilters()['language'][0]);

  SearchModel.toggle_filter('something', 'test');
  assert.ok(SearchModel.facetfilters()['something']);
  assert.equal('test', SearchModel.facetfilters()['something'][0]);
});

QUnit.test('filter_exists', function( assert ) {
  SearchModel.facetfilters({});
  SearchModel.toggle_filter('language', 'test');
  assert.ok(SearchModel.filter_exists('language', 'test'));
  assert.notOk(SearchModel.filter_exists('language', 'test2'));

  SearchModel.toggle_filter('repo', 'test');
  assert.ok(SearchModel.filter_exists('repo', 'test'));
  assert.notOk(SearchModel.filter_exists('repo', 'test2'));

  SearchModel.toggle_filter('owner', 'test');
  assert.ok(SearchModel.filter_exists('owner', 'test'));
  assert.notOk(SearchModel.filter_exists('owner', 'test2'));
});

QUnit.test('get_lang_url_filters', function( assert ) {
  SearchModel.facetfilters({});
  SearchModel.toggle_filter('language', 'test1');
  assert.equal(SearchModel.get_lang_url_filters(), '&lan=test1');
  SearchModel.toggle_filter('language', 'test2');
  assert.equal(SearchModel.get_lang_url_filters(), '&lan=test1&lan=test2');
});

QUnit.test('get_repo_url_filters', function( assert ) {
  SearchModel.facetfilters({});
  SearchModel.toggle_filter('repo', 'test1');
  assert.equal(SearchModel.get_repo_url_filters(), '&repo=test1');
  SearchModel.toggle_filter('repo', 'test2');
  assert.equal(SearchModel.get_repo_url_filters(), '&repo=test1&repo=test2');
});

QUnit.test('get_own_url_filters', function( assert ) {
  SearchModel.facetfilters({});
  SearchModel.toggle_filter('owner', 'test1');
  assert.equal(SearchModel.get_own_url_filters(), '&own=test1');
  SearchModel.toggle_filter('owner', 'test2');
  assert.equal(SearchModel.get_own_url_filters(), '&own=test1&own=test2');
});

QUnit.test('get_src_url_filters', function( assert ) {
  SearchModel.facetfilters({});
  SearchModel.toggle_filter('source', 'test1');
  assert.equal(SearchModel.get_src_url_filters(), '&src=test1');
  SearchModel.toggle_filter('source', 'test2');
  assert.equal(SearchModel.get_src_url_filters(), '&src=test1&src=test2');
});

QUnit.test('get_path_url_filters', function( assert ) {
  SearchModel.pathvalue('');
  assert.equal(SearchModel.get_path_url_filters(), '');
  SearchModel.pathvalue('terrywashere');
  assert.equal(SearchModel.get_path_url_filters(), '&fl=terrywashere');
});

QUnit.test('get_lit_url_filter', function( assert ) {
  SearchModel.literalview(false);
  assert.equal(SearchModel.get_lit_url_filter(), '');
  SearchModel.literalview(true);
  assert.equal(SearchModel.get_lit_url_filter(), '&lit=true');
});

// QUnit.test('setstatechange', function( assert ) {
//   SearchModel.searchvalue('terrywashere')
//   SearchModel.setstatechange(0, true);
//   assert.equal('terrywashere', history.state.searchvalue);  
  
//   SearchModel.setstatechange(0, undefined);
//   assert.notEqual(length, history.length);
//   assert.equal('terrywashere', history.state.searchvalue);
// });

QUnit.test('get_search_query_string', function( assert ) {
  SearchModel.facetfilters({});

  SearchModel.pathvalue('');
  SearchModel.searchvalue('terrywashere');
  
  var res = SearchModel.get_search_query_string();
  assert.equal(res, '?q=terrywashere&p=0');

  SearchModel.pathvalue('thisismypath');
  SearchModel.searchvalue('terrywashere');
  SearchModel.toggle_filter('language', 'one');
  SearchModel.toggle_filter('language', 'two');
  SearchModel.toggle_filter('repo', 'three');
  SearchModel.toggle_filter('repo', 'four');
  SearchModel.toggle_filter('owner', 'seven');
  SearchModel.toggle_filter('owner', 'eight');
  SearchModel.toggle_filter('source', 'five');
  SearchModel.toggle_filter('source', 'six');
  
  var res = SearchModel.get_search_query_string();
  assert.equal(res, '?q=terrywashere&lan=one&lan=two&repo=three&repo=four&own=seven&own=eight&src=five&src=six&fl=thisismypath&p=0');
});

/////////////////////////////////////////////////////////////////////
QUnit.module('SearchComponent');
QUnit.test('view', function( assert ) {
  var result = SearchComponent.view();
  assert.equal(result.tag, 'div');
  assert.equal(result.children.length, 4);
  assert.equal(result.children[3].tag, 'div');
});

QUnit.module('SearchNoResultsComponent');
QUnit.test('view', function( assert ) {
  var result = SearchNoResultsComponent.view(null, {totalhits: 10});
  assert.equal(result.tag, 'div');
  assert.equal(result.children.length, 0);

  result = SearchNoResultsComponent.view(null, {totalhits: 0, altquery: [], query: 'my query'});
  assert.equal(result.children[0].tag, 'h4')
  assert.ok(result.children[0].children[0].indexOf('No results found for ') !== -1);
  assert.equal(result.children[0].children[1].tag, 'i')
  assert.ok(result.children[0].children[1].children[0].indexOf('my query') !== -1);

  result = SearchNoResultsComponent.view(null, {totalhits: 0, altquery: ['one', 'two'], query: 'my query'});
  
  assert.deepEqual(result.children[1].children[0], {
    "attrs": {},
    "children": [
      "Try one of the following searches instead"
    ],
    "tag": "h5"
  });

  assert.deepEqual(result.children[1].children[1].children[0].children[0].children[0], 'one');
  assert.deepEqual(result.children[1].children[1].children[1].children[0].children[0], 'two');
});

QUnit.test('controller', function( assert ) {
  var result = SearchNoResultsComponent.controller();
  var called = false;
  SearchModel.search = function() { called = true; };
  result.doaltquery('myquery');

  assert.equal('myquery', SearchModel.searchvalue());
  assert.ok(called);
});

/////////////////////////////////////////////////////////////////////
QUnit.module('SearchLoadingComponent');
QUnit.test('view', function( assert ) {
  SearchModel.currentlyloading(false);
  var result = SearchLoadingComponent.view();
  
  assert.deepEqual(result, {
    "attrs": {
      "className": "search-loading",
      "style": {
        "display": "none"
      }
    },
    "children": [
      {
        "attrs": {
          "src": "/img/loading.gif"
        },
        "children": [],
        "tag": "img"
      },
      {
        "attrs": {},
        "children": [
          "Loading..."
        ],
        "tag": "h5"
      }
    ],
    "tag": "div"
  });

  SearchModel.currentlyloading(true);
  var result = SearchLoadingComponent.view();
  
  assert.deepEqual(result, {
    "attrs": {
      "className": "search-loading"
    },
    "children": [
      {
        "attrs": {
          "src": "/img/loading.gif"
        },
        "children": [],
        "tag": "img"
      },
      {
        "attrs": {},
        "children": [
          "Loading..."
        ],
        "tag": "h5"
      }
    ],
    "tag": "div"
  });
});

/////////////////////////////////////////////////////////////////////
QUnit.module('SearchResultsComponent');
QUnit.test('controller', function( assert ) {
  var controller = SearchResultsComponent.controller();
  
  var input = { 'codeId': '999', 'codePath': 'zepath' }
  var result = controller.gethref(input);
  assert.equal(result, '/file/999/zepath');

  input = { 'repoName': 'my-repository-name' }
  result = controller.getrepositoryhref(input);
  assert.equal(result, '/repository/overview/my-repository-name/');

  input = { 'codeId': '999', 'codePath': 'zepath'}
  result = controller.gethreflineno(input, 1337);
  assert.equal(result, '/file/999/zepath#1337');

  input = { 'codeLines': '99', 'languageName': 'Kotlin' }
  result = controller.getsmallvalue(input);
  assert.equal(result, ' | 99 lines | Kotlin');

  input = { 'fileName': 'bingo.java' }
  result = controller.getatag(input);
  assert.equal(result, 'bingo.java');
});

</script>
</body>
</html>